package com.wyw.hemerocallis.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 抽象权限过滤器, 可以有多种实现
 *
 * @author Mr Wu    yewen.wu.china@gmail.com
 * <p>
 * Update History:
 * Author        Time            Content
 */
@Slf4j
public abstract class AbsSecurityFilter implements GatewayFilter, Ordered {


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        RequestContext context = RequestContext.getCurrentContext();
//        HttpServletRequest request = context.getRequest();
//        HttpServletResponse response = context.getResponse();
//
//        log.info("filter {} begin check request {}.",
//                this.getClass().getSimpleName(), request.getRequestURI());
//
//        Boolean result = null;
//        try {
//            result = interceptCheck(request, response);
//        } catch (Exception ex) {
//            log.error("filter {} check request {}, throws exception {}.",
//                    this.getClass().getSimpleName(),
//                    request.getRequestURI(), ex.getMessage());
//        }
//
//        log.info("filter {} finish check, result {}",
//                this.getClass().getSimpleName(), result);
//
//        if (result == null) {
//            log.debug("Filter {} finish check, result is null.",
//                    this.getClass().getSimpleName());
//
//            // 对当前的请求不进行路由
//            context.setSendZuulResponse(false);
//            context.setResponseStatusCode(getHttpStatus());
//            return null;
//        }
//
//        if (!result) {
//
//            try {
//                // 对当前的请求不进行路由
//                context.setSendZuulResponse(false);
//                context.setResponseStatusCode(getHttpStatus());
//                response.setHeader("Content-type",
//                        "application/json;charset=UTF-8");
//                response.setCharacterEncoding("UTF-8");
//                response.getWriter().write(getErrorMsg());
//                context.setResponse(response);
//            } catch (IOException ex) {
//                log.error("filter {} check request {}, result is false," +
//                                "setResponse throws Exception {}",
//                        this.getClass().getSimpleName(),
//                        request.getRequestURI(), ex.getMessage());
//            }
//        }
//
//        return null;

        //从Http Header 中寻找key为token，value 为wyw的键值对
        String name = exchange.getRequest().getHeaders().getFirst("token");
        if("wyw".equals(name)){
            return chain.filter(exchange);
        }

        //标记此次请求没有权限，并结束这次请求
        exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
        return exchange.getResponse().setComplete();
    }

    @Override
    public int getOrder() {
        return HIGHEST_PRECEDENCE + 3;
    }
    /**
     * 子 Filter 实现该方法, 填充校验逻辑
     * @return true: 通过校验; false: 校验未通过
     * */
    protected abstract Boolean interceptCheck(HttpServletRequest request,
                                              HttpServletResponse response)
            throws Exception;

    protected abstract int getHttpStatus();

    protected abstract String getErrorMsg();

}
